package com.aishua.andpaydemo.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aishua.andpaydemo.R;
import com.aishua.andpaydemo.ThemeHelper;
import com.aishua.andpaydemo.adapter.ChoiceColorAdapter;
import com.aishua.andpaydemo.utils.LogUtils;
import com.bilibili.magicasakura.utils.ThemeUtils;


/**
 * Created by aishua on 2018/5/17.
 */

public class ChoiceColorActivity extends AppCompatActivity {
    private int mCurrentTheme;
    private RecyclerView rl_choicecolor;
    private ImageView img_title_xiaoxi;
    private TextView tv_title;
    private TextView tv_setting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choicecolor);

        initTitleView();
        initView();
        initData();
    }

    private void initTitleView() {

        img_title_xiaoxi = findViewById(R.id.img_title_xiaoxi);
        img_title_xiaoxi.setVisibility(View.INVISIBLE);
        tv_title = findViewById(R.id.tv_title);
        tv_setting = findViewById(R.id.tv_setting);
        tv_title.setText("主题皮肤");
        tv_setting.setText("完成");
        tv_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

    }

    private void initView() {

        rl_choicecolor = findViewById(R.id.rl_choicecolor);

    }

    private void initData() {


        String[] colorname = new String[]{"默认主题", "闪耀黄", "樱花粉", "烈焰红", "青草绿", "海天蓝"};
        int[] cloltID = new int[]{getResources().getColor(R.color.purple), getResources().getColor(R.color.yellow), getResources().getColor(R.color.pink), getResources().getColor(R.color.red), getResources().getColor(R.color.green), getResources().getColor(R.color.blue)};

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
        rl_choicecolor.setLayoutManager(layoutManager);
        rl_choicecolor.setItemAnimator(new DefaultItemAnimator());
        ChoiceColorAdapter choiceColorAdapter = new ChoiceColorAdapter(this, colorname, cloltID);
        rl_choicecolor.setAdapter(choiceColorAdapter);

        choiceColorAdapter.setonRecycleViewClickListener(new ChoiceColorAdapter.onRecycleViewClickListener() {
            @Override
            public void onRecycleViewClick(int position) {

//                Toast.makeText(ChoiceColorActivity.this, "positon:" + position, Toast.LENGTH_SHORT).show();
                changeTheme(position);


            }
        });
    }

    public void changeTheme(int position) {
        switch (position) {

            case 0:
                mCurrentTheme = ThemeHelper.PURPLE;

                break;
            case 1:
                mCurrentTheme = ThemeHelper.YELLOW;

                break;
            case 2:
                mCurrentTheme = ThemeHelper.PINK;

                break;
            case 3:
                mCurrentTheme = ThemeHelper.RED;

                break;
            case 4:
                mCurrentTheme = ThemeHelper.GREEN;

                break;
            case 5:
                mCurrentTheme = ThemeHelper.BLUE;
                break;
        }
        change();
//        EventBus.getDefault().post("theme_change");
    }


    public void change() {

        int currentTheme = mCurrentTheme;
        if (ThemeHelper.getTheme(ChoiceColorActivity.this) != currentTheme) {
            ThemeHelper.setTheme(ChoiceColorActivity.this, currentTheme);
            ThemeUtils.refreshUI(ChoiceColorActivity.this);
        }
    }
}
